/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MapEditor;

import YottaTron.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.*;

public class MainFrame extends JFrame implements ActionListener, MouseWheelListener, MouseMotionListener {

    String graphicsDir = "/YottaTron/Graphics/";
    MapObjectsMenu objectsMenu;
    MapPanel mapPanel;
    JScrollPane spMap, spMenu;
    JPanel ovladaniP, pMenu;
    private JButton bSave,  bLoad;
    private JLabel lFile;
    private JTextField tfFile,  tfHeight,  tfWidth,  tfTilesHeight,  tfTilesWidth;
    public JTextField tfBackground;
    //private JTextField tfZoom;
    //private JLabel lZoom;
    private JLabel lHeight,  lWidth,  lTilesHeight,  lTilesWidth,  lBackground;
    public GraphicsLoader[] graphics;
    public GraphicsLoader[][] destructibleObstacleGraphics;

    public MainFrame(String label, int width, int height) {
        super(label);
        try {
            tfBackground = new JTextField("background1.png");
            //nacteni grafiky
            GraphicsLoader gBackground = new GraphicsLoader(graphicsDir + "backgrounds/" + tfBackground.getText());
            GraphicsLoader gRock = new GraphicsLoader(graphicsDir + "rock.png");
            RotatingGraphicsLoader grTree0 = new RotatingGraphicsLoader(graphicsDir + "tree0.png");
            RotatingGraphicsLoader grTree1 = new RotatingGraphicsLoader(graphicsDir + "tree1.png");
            RotatingGraphicsLoader grTree2 = new RotatingGraphicsLoader(graphicsDir + "tree2.png");
            RotatingGraphicsLoader grTree3 = new RotatingGraphicsLoader(graphicsDir + "tree3.png");
            RotatingGraphicsLoader grTree4 = new RotatingGraphicsLoader(graphicsDir + "tree4.png");
            GraphicsLoader[] gTree = new GraphicsLoader[5];
            // zde bude od kazde
            graphics = new GraphicsLoader[15];
            // pozor, kazda konstanta musi byt jina, aby se grafika nepremazala
            graphics[Runner.GST_BACKGROUND1] = gBackground;
            graphics[Runner.GSP_ROCK] = gRock;
            graphics[Runner.GSP_TREE0] = grTree0;
            graphics[Runner.GSP_TREE1] = grTree1;
            graphics[Runner.GSP_TREE2] = grTree2;
            graphics[Runner.GSP_TREE3] = grTree3;
            graphics[Runner.GSP_TREE4] = grTree4;
            //------------------------------- konec grafiky
            this.setSize(new Dimension(width, height));
            //this.setLayout(new GridBagLayout());
            this.setLayout(new BorderLayout());
            // prvky GUI
            objectsMenu = new MapObjectsMenu(this);
            bSave = new JButton("ulozit");
            bLoad = new JButton("nacist");

            tfFile = new JTextField("mapa1.map");
            tfHeight = new JTextField("500");
            tfWidth = new JTextField("500");
            tfTilesHeight = new JTextField("10");
            tfTilesWidth = new JTextField("10");

            lFile = new JLabel("nazev souboru:");
            lHeight = new JLabel("vyska mapy");
            lWidth = new JLabel("sirka mapy");
            lTilesHeight = new JLabel("vyska tiles");
            lTilesWidth = new JLabel("sirka tiles");
            lBackground = new JLabel("/Graphics/backgrounds/");

            ovladaniP = new JPanel();
            ovladaniP.setLayout(new BoxLayout(ovladaniP, BoxLayout.Y_AXIS));
            ovladaniP.setPreferredSize(new Dimension(150,150));
            pMenu = new JPanel(new BorderLayout());
            ovladaniP.setPreferredSize(new Dimension(300,150));
            mapPanel = new MapPanel(Integer.decode(tfWidth.getText()), Integer.decode(tfHeight.getText()), this);

            spMap = new JScrollPane(mapPanel);
            spMenu = new JScrollPane(objectsMenu);

            // vlozeni prvku ovladani do panelu
            ovladaniP.add(bSave);
            ovladaniP.add(bLoad);
            ovladaniP.add(lFile);
            ovladaniP.add(tfFile);
            ovladaniP.add(lHeight);
            ovladaniP.add(tfHeight);
            ovladaniP.add(lWidth);
            ovladaniP.add(tfWidth);
            ovladaniP.add(lTilesHeight);
            ovladaniP.add(tfTilesHeight);
            ovladaniP.add(lTilesWidth);
            ovladaniP.add(tfTilesWidth);
            ovladaniP.add(lBackground);
            ovladaniP.add(tfBackground);
            //ovladaniP.add(lZoom);
            //ovladaniP.add(tfZoom);
            tfFile.setMaximumSize(new Dimension(150, 20));
            tfHeight.setMaximumSize(new Dimension(150, 20));
            tfWidth.setMaximumSize(new Dimension(150, 20));
            tfBackground.setMaximumSize(new Dimension(150, 20));
            tfTilesHeight.setMaximumSize(new Dimension(150, 20));
            tfTilesWidth.setMaximumSize(new Dimension(150, 20));

            //tfZoom.setMaximumSize(new Dimension(150, 20));
            // pridani prvku do okna
            ovladaniP.add(spMenu, BorderLayout.SOUTH);
            this.add(ovladaniP, BorderLayout.WEST);
            mapPanel.setBounds(100, 10, mapPanel.width, mapPanel.height);
            this.add(spMap, BorderLayout.CENTER);
            //this.add(spMenu, BorderLayout.CENTER);
            //System.out.println(scrollPane.g);
            // registrace posluchacu
            bSave.addActionListener(this);
            bLoad.addActionListener(this);
            tfHeight.addActionListener(this);
            tfWidth.addActionListener(this);
            tfBackground.addActionListener(this);
            //spMap.addMouseWheelListener(this);
            spMap.addMouseWheelListener(this);
            spMenu.addMouseWheelListener(this);
            //tfZoom.addActionListener(this);
            // rychlost scrollovani je obstarana uvnitr mouseWheelMoved
            spMap.setWheelScrollingEnabled(false);
            spMenu.setWheelScrollingEnabled(false);
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(bSave)) {
            String jmeno = tfFile.getText();
            mapPanel.map.save(jmeno);
        }
        if (e.getSource().equals(bLoad)) {
            String jmeno = tfFile.getText();
            mapPanel.map.load(jmeno);
            tfBackground.setText(mapPanel.map.backgroundName);
            GraphicsLoader gBackground = null;
            try {
                gBackground = new GraphicsLoader(graphicsDir + "backgrounds/" + tfBackground.getText());
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            graphics[Runner.GST_BACKGROUND1] = gBackground;
            mapPanel.repaint();
        }
        if (e.getSource().equals(tfHeight)) {
            mapPanel.height = Integer.valueOf(tfHeight.getText());
            mapPanel.setSize();
        }
        if (e.getSource().equals(tfWidth)) {
            mapPanel.width = Integer.valueOf(tfWidth.getText());
            mapPanel.setSize();
        }
        if (e.getSource().equals(tfBackground)) {
            try {
                System.out.println("changing background");
                tfBackground.setBackground(Color.WHITE);
                GraphicsLoader gBackground = new GraphicsLoader(graphicsDir + "backgrounds/" + tfBackground.getText());
                graphics[Runner.GST_BACKGROUND1] = gBackground;
            } catch (IOException ex) {
                tfBackground.setBackground(Color.RED);
                System.out.println("soubor nenalezen");
            }
            mapPanel.repaint();
        }
//        if (e.getSource().equals(tfZoom)) {
//            double zoom = Double.valueOf(tfZoom.getText());
//            mapPanel.setZoom(zoom);
//        }
    }

    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getSource().equals(spMap)) {
            if (e.isShiftDown()) {
                spMap.getHorizontalScrollBar().setValue(spMap.getHorizontalScrollBar().getValue() + e.getWheelRotation() * 100);
            } else {
                spMap.getVerticalScrollBar().setValue(spMap.getVerticalScrollBar().getValue() + e.getWheelRotation() * 100);
            }
        }
        if (e.getSource().equals(spMenu)) {
            if (e.isShiftDown()) {
                spMenu.getHorizontalScrollBar().setValue(spMenu.getHorizontalScrollBar().getValue() + e.getWheelRotation() * 100);
            } else {
                spMenu.getVerticalScrollBar().setValue(spMenu.getVerticalScrollBar().getValue() + e.getWheelRotation() * 100);
            }
        }
    }

    public static void main(String[] args) {
        // hlavni okno aplikace
        MainFrame okno = new MainFrame("RoboCon Editor", 900, 700);


        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        okno.setVisible(true);
    }

    public void mouseDragged(MouseEvent e) {
        mapPanel.repaint();
    }

    public void mouseMoved(MouseEvent e) {
    }
}
